Skip to content

[SYCLomatic] If viewed from the translation unit, the header file <bits/stdc++.h> is included before SYCL header file, then insert the SYCL header file at the beginning of the main source file of the translation unit. #2790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: SYCLomatic
Choose a base branch
from

Conversation

zhiweij1
Copy link
Contributor

@zhiweij1 zhiweij1 commented Apr 16, 2025

Background: if <bits/stdc++.h> is included before the SYCL header file, it results in a compiler error.

The behavior before this PR:
If a file contains CUDA syntax, the tool inserts the SYCL header file in that file. Also, SYCL header file needs to be inserted at the beginning of the main file (to avoid bits/stdc++.h is included before SYCL header file). The solution has an issue: sycl.hpp may be inserted in some pure C++ code without checking if bits/stdc++.h is included or not.

This PR will be further refined as follows:
In the translation unit, if bits/stdc++.h is included before the SYCL header file, insert the SYCL header file at the beginning of the main file.

…ncluded, don't insert sycl.hpp at the mainfile

Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
@zhiweij1 zhiweij1 requested a review from a team as a code owner April 16, 2025 09:01
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
Copy link
Contributor

@tomflinda tomflinda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhiweij1 zhiweij1 changed the title [SYCLomatic] If <bits/stdc++.h> is included after sycl.hpp or isn't included, don't insert sycl.hpp at the mainfile [SYCLomatic] If <bits/stdc++.h> is included before the inserted sycl.hpp, also insert sycl.hpp at the beginning of main file Apr 22, 2025
@zhimingwang36 zhimingwang36 changed the title [SYCLomatic] If <bits/stdc++.h> is included before the inserted sycl.hpp, also insert sycl.hpp at the beginning of main file [SYCLomatic] If viewed from the translation unit, the header file <bits/stdc++.h> is included before SYCL header file, then insert the SYCL header file at the beginning of the main source file of the translation unit. Apr 28, 2025
@zhiweij1 zhiweij1 requested a review from Copilot May 16, 2025 01:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refines the logic for inserting the SYCL header by ensuring it is only added to the main source file when <bits/stdc++.h> is included before SYCL headers, and it also removes several redundant test cleanup commands. The key changes are:

  • Removal of the commented-out file removal commands from various test files.
  • Addition of a flag and mapping update when <bits/stdc++.h> is encountered in the preprocessor callbacks.
  • Updating the global state and header insertion logic in AnalysisInfo to conditionally insert SYCL headers based on inclusion order.

Reviewed Changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 2 comments.

File Description
clang/test/dpct/*.cpp Removed commented out test cleanup commands
clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp Added check to set flag when <bits/stdc++.h> is included
clang/lib/DPCT/PreProcessor.cpp Updated to record files included after encountering <bits/stdc++.h>
clang/lib/DPCT/AnalysisInfo.{h,cpp} Adjusted header insertion logic and flag reset in the main file setter

@@ -132,6 +132,10 @@ void IncludesCallbacks::InclusionDirective(
FileInfo->setFirstIncludeOffset(LocInfo.second);
LastInclusionLocationUpdater Updater(FileInfo, FilenameRange.getEnd());

if (FileName == "bits/stdc++.h") {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the literal "bits/stdc++.h" into a named constant to improve maintainability and reduce the chances of typos in future modifications.

Suggested change
if (FileName == "bits/stdc++.h") {
if (FileName == BITS_STDCXX_HEADER) {

Copilot uses AI. Check for mistakes.

@@ -1403,7 +1409,10 @@ class DpctGlobalInfo {
return findObject(FileMap, FilePath);
}
std::shared_ptr<DpctFileInfo> getMainFile() const { return MainFile; }
void setMainFile(std::shared_ptr<DpctFileInfo> Main) { MainFile = Main; }
void setMainFile(std::shared_ptr<DpctFileInfo> Main) {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It would be helpful to add a brief comment in setMainFile explaining why the IsAfterBitsStdcxx flag is reset when a new main file is set, to clarify the logic for future maintainers.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants